#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *thread(void *argv)
{
    int *p_num = argv;
    pthread_mutex_lock(&lock);
    *p_num += 1;
    pthread_mutex_unlock(&lock);
    return NULL;
}

int main(int argc, char const *argv[])
{
    int num = 0;
    pthread_t threads[20000];

    for (int i = 0; i < 20000; i++)
    {
        pthread_create(threads + i, NULL, thread, &num);
    }

    for (int i = 0; i < 20000; i++)
    {
        pthread_join(threads[i], NULL);
    }

    // for (int i = 0; i < 20000; i++)
    // {
    //     num++;
    // }

    printf("num: %d\n", num);

    return 0;
}
